package com.haisen.dataobject.entity;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author cjbi
 */
@Table(name = "sys_user")
@Data
public class User {

    /**
     * 编号
     */
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    /**
     * 所属公司
     */
    @NotNull(message = "所属组织不能为空")
    private Long organizationId;
    /**
     * 用户名
     */
    @NotBlank(message = "用户名不能为空")
    @Min(value = 3,message = "用户名不能低于3位")
    private String username;
    /**
     * 密码
     */
    @NotBlank(message = "密码不能为空")
    @Min(value = 6,message = "密码不能低于6位")
    private String password;
    /**
     * 加密密码的盐
     */
    private String salt;
    /**
     *用户昵称
     */
    private String nickName;
    /**
     * 电话
     */
    private String phone;
    /**
     * 用户图像
     */
    private String headImag;
    /**
     * 拥有的角色列表
     */
    private String roleIds;

    /**
     * 如果有get方法,而属性上增加@Ttransient属性,这会生成字段
     */
    @Transient
    private List<Long> roleIdList;

    private String groupIds;

    @Transient
    private List<Long> groupIdList;
    /**
     * 是否部门主管，Map结构的json字符串，key是部门的Id，value是人员在这个部门中是否为主管，true表示是，
     * false表示不是
     */
    private String leaderInDepts;

    /**
     * 是部门主管的部门ids
     */
    @Transient
    private List<String> leadInDeptsList;

    private Boolean locked = Boolean.FALSE;

    //钉钉userid，根据userid和accesstoken可以获取用户详情
    private String dingUserId;

    public User() {
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }


    public String getCredentialsSalt() {
        return username + salt;
    }

    /**
     * 更新ids时，同时更新IdList;通过此中方式解决对象包含关系,使用的是id，非对象关联
     * @param roleIds
     */
    public void setRoleIds(String roleIds) {
        String[] roleIdStrs = roleIds.split(",");
        for (String roleId : roleIdStrs) {
            if (StringUtils.isEmpty(roleId)) {
                continue;
            }
            getRoleIdList().add(Long.valueOf(roleId));
        }
        this.roleIds = roleIds;
    }

    public List<Long> getRoleIdList() {
        if (roleIdList == null) {
            roleIdList = new ArrayList<>();
        }
        return roleIdList;
    }

    /**
     *  设置roleIdList时，同时更新ids
     * @param roleIdList
     */
    public void setRoleIdList(List<Long> roleIdList) {
        StringBuilder s = new StringBuilder();
        for (Long roleId : roleIdList) {
            s.append(roleId);
            s.append(",");
        }
        if (s.length() > 0) {
            s.deleteCharAt(s.length() - 1);
        }
        this.roleIds = s.toString();
        this.roleIdList = roleIdList;
    }

    public String getGroupIds() {
        return groupIds;
    }

    public void setGroupIds(String groupIds) {
        String[] groupIdStrs = groupIds.split(",");
        for (String groupId : groupIdStrs) {
            if (StringUtils.isEmpty(groupId)) {
                continue;
            }
            getGroupIdList().add(Long.valueOf(groupId));
        }
        this.groupIds = groupIds;
    }

    public List<Long> getGroupIdList() {
        if (groupIdList == null) {
            groupIdList = new ArrayList<>();
        }
        return groupIdList;
    }

    public void setGroupIdList(List<Long> groupIdList) {
        StringBuilder s = new StringBuilder();
        for (Long groupId : groupIdList) {
            s.append(groupId);
            s.append(",");
        }
        if (s.length() > 0) {
            s.deleteCharAt(s.length() - 1);
        }
        this.groupIds = s.toString();
        this.groupIdList = groupIdList;
    }

    public String getLeaderInDepts() {
        return leaderInDepts;
    }

    public void setLeaderInDepts(String leaderInDepts) {
        List<String> deptIds = new ArrayList<>();
        Map<String,Boolean> leadInDeptsMap = JSON.parseObject(leaderInDepts, Map.class);
        for (Map.Entry<String, Boolean> entry : leadInDeptsMap.entrySet()) {
            if (entry.getValue()){
                deptIds.add(String.valueOf(entry.getKey()));
            }
        }
        this.leaderInDepts = leaderInDepts;
        this.leadInDeptsList = deptIds;
    }


    public List<String> getLeadInDeptsList() {
        if (leadInDeptsList == null){
            leadInDeptsList = new ArrayList<>();
        }
        return leadInDeptsList;
    }

    public void setLeadInDeptsList(List<String> leadInDeptsList) {
        this.leadInDeptsList = leadInDeptsList;
    }

    public Boolean getLocked() {
        return locked;
    }

    public void setLocked(Boolean locked) {
        this.locked = locked;
    }

}
